<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script src="../js/promise_ajax.js"></script>

    <script>

        // race 比赛 
        // Promise.race([])   参数是一个数组，数组中的值必须是promise实例
        //    谁先返回数据，就把谁的结果作为最终的结果,无论成功或者失败


        // const p1 = ajax('get', '../php/1.php')

        // const p2 = ajax('get', '../php/2.php')

        // const p3 = ajax('get', '../php/3.php')


        // const p = Promise.race([p1, p2, p3]);
        // p.then(res => {
        //     console.log(p);
        // })


        const pp1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100);
                n % 2 ? resolve(n + '第一次成功') : reject(n + '第一次失败');
            }, 100)
        })

        const pp2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100);
                n % 2 ? resolve(n) : reject(n + '第2次失败');
            })
        })

        const pp3 = new Promise((resolve, reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100);
                n % 2 ? resolve(n) : reject(n + '第3次失败');
            })
        })


        const pp = Promise.race([pp1, pp2, pp3]);
        pp.then(
            res => {
                console.log(res);
            },
            err => {
                console.log(err);
            }
        )





    </script>

</body>

</html>